package cn.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ExceResult;
import cn.common.GridCommon;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.common.utils.ChineseCharToPingYinUtils;
import cn.kpi.entity.sys.GeneralCodeEntity;
import cn.kpi.entity.sys.RoleEntity;
import cn.kpi.service.sys.IGeneralCodeService;

@Controller
@RequestMapping("/generalcode")
public class GeneralCodeController extends BaseController {
	
	@Autowired
	private IGeneralCodeService iGeneralCodeService;
	
	@RequestMapping(value = "/generalcode_list")
	public String GeneralCodeList(Model model) {
		
		try{
			List<GeneralCodeEntity> lst=iGeneralCodeService.GetGeneralCodeList();
			model.addAttribute("lst", lst);
		}
		catch(Exception ex){
			
		}
		return "sys/generalcode_list";
	}
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public LayTable<GeneralCodeEntity> GetList(HttpServletRequest request) {

		List<GeneralCodeEntity> lst=null;
		LayTable<GeneralCodeEntity> layTable=new LayTable<GeneralCodeEntity>();
		try {
			//String RoleName = request.getParameter("rolename");
			
			int page=Integer.parseInt(request.getParameter("page"));
			int limit=Integer.parseInt(request.getParameter("limit"));
			int beginrow=(page-1)*limit;
			int endrow=limit;
			
			lst = iGeneralCodeService.GetGeneralCodeListLimit(beginrow,endrow);
			for (int i = 0; i < lst.size(); i++) {
				lst.get(i).setRowNum(i + 1);
			}

			layTable.setCode(0);
			layTable.setCount(iGeneralCodeService.GetGeneralCodeListLimitCount());
			layTable.setData(lst);
			
		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}

	
	@RequestMapping(value = "/add_generalcode")
	public String AddGeneralCode(HttpServletRequest request,HttpServletResponse response){
		try{
			GeneralCodeEntity entity=new GeneralCodeEntity();
			entity.setGeneralid(Long.parseLong(request.getParameter("generalid")));
			entity.setParentid(Long.parseLong(request.getParameter("parentid")));
			GeneralCodeEntity parent=iGeneralCodeService.GetGeneralCodeById(entity.getParentid());
			entity.setCodedes(request.getParameter("codedes"));
			entity.setCodename(request.getParameter("codename"));
			if(request.getParameter("enableflag").equals("1")){
				entity.setEnableflag(true);
			}
			else{
				entity.setEnableflag(false);
			}
			
			entity.setGeneralcode(request.getParameter("generalcode"));
			entity.setParentcode(request.getParameter("parentcode"));
			if(entity.getParentid()>0){
				entity.setCodelevel(parent.getCodelevel()+1);
				entity.setParentcode(parent.getGeneralcode());
			}
			else{
				entity.setCodelevel(0);
			}
			
			ChineseCharToPingYinUtils cte=new ChineseCharToPingYinUtils();
			String szm=cte.getAllFirstLetter(entity.getCodename());
			entity.setKeyword(szm);
			entity.setRemark(request.getParameter("remark"));
			
			
			
			int iResult=0;
			if(entity.getGeneralid()>0){
				iResult=iGeneralCodeService.UpdateEntity(entity);
			}
			else{
				iResult=iGeneralCodeService.InsertEntity(entity);	
			}
			
			return renderStringScript(response,"<script>window.parent.CloseDialog("+iResult+")</script>");
		}
		catch(Exception ex){
			return renderStringScript(response,"<script>window.parent.CloseDialog(0)</script>");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete_generalcode")
	public ExceResult DeleteGeneralCode(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long generalid=Long.parseLong(request.getParameter("generalid"));
		 int iResult=iGeneralCodeService.DeleteEntity(generalid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
}
